const {
	Controller
} = require('uni-cloud-router')
const uniID = require('uni-id')
const dbHelper = require('db-helper')
module.exports = class GoodsController extends Controller {
	async syncHomeModuleCache() {
		let {
			city
		} = this.ctx.data
		if (!city) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try{
			await this.service.goods.syncHomeModuleCache(city)
			return {
				code: 200
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async getReceiptNames() {
		let {
			storeId
		} = this.ctx.data
		try{
			let list = await this.service.goods.getReceiptNames({
				storeId
			})
			return {
				code: 200,
				result: list
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async giveTicketGift() {
		let {
			skuId,
			storeId,
			count,
			memberId
		} = this.ctx.data
		if (!skuId || !storeId || !count || !parseInt(count) || parseInt(count) < 1 || !memberId || !parseInt(memberId)) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try{
			await this.service.goods.giveTicketGift({
				skuId,
				storeId,
				count,
				memberId
			})
			return {
				code: 200
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async getTicketPackageList() {
		let {
			storeId,
			pageNum,
			pageSize
		} = this.ctx.data
		if (!storeId || (pageNum && !parseInt(pageNum)) || (pageSize && !parseInt(pageSize))) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try{
			let page = await this.service.goods.getTicketPackageList({
				storeId,
				pageNum,
				pageSize
			})
			return {
				code: 200,
				result: page
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async clearPosterCache() {
		let {
			goodsId
		} = this.ctx.data
		try{
			let count = await this.service.goods.clearPosterCache(goodsId)
			return {
				code: 200,
				msg: `成功删除${count}条缓存`
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async getExchangeCodeGroupList() {
		let {
			pageNum,
			pageSize,
			city
		} = this.ctx.data
		try{
			let res = await this.service.goods.getExchangeCodeGroupList({
				pageNum,
				pageSize,
				city
			})
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async getGoodsList() {
		let {
			keyword,
			order,
			pageNum,
			pageSize,
			uid,
			channel,
			isOnSell,
			auditStatus,
			city,
			isTimeLimited,
			inWarehouse
			
		} = this.ctx.data
		
		try{
			let res = await this.service.goods.getGoodsList({
				uid,
				channel,
				keyword,
				order,
				pageNum,
				pageSize,
				isOnSell,
				auditStatus,
				city,
				isTimeLimited,
				inWarehouse
			})
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async getSoldGoodsList() {
		let {
			startDate,
			endDate, 
			keyword
		} = this.ctx.data
		if ((startDate && !parseInt(startDate))
			|| (endDate && !parseInt(endDate))) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try{
			let result = await this.service.goods.getSoldGoodsList({
				startDate,
				endDate, 
				keyword
			})
			return {
				code: 200,
				result
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async getGoodsProfit() {
		let {
			startDate,
			endDate, 
			goodsId
		} = this.ctx.data
		if (!goodsId || (startDate && !parseInt(startDate))
			|| (endDate && !parseInt(endDate))) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try{
			let result = await this.service.goods.getGoodsProfit({
				startDate,
				endDate, 
				goodsId
			})
			return {
				code: 200,
				result
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async getGoodsProfitList() {
		let {
			startDate,
			endDate, 
			order, 
			keyword,
			city = '4201'
		} = this.ctx.data
		if ((startDate && !parseInt(startDate))
			|| (endDate && !parseInt(endDate)) 
			|| (order && order != 1 && order != -1)) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try{
			let isRegionMgr = this.ctx.auth.role.some(role => {
				return role === 'region_mgr'
			})
			if (isRegionMgr) {
				let userRes = await this.db.collection("uni-id-users").doc(this.ctx.auth.uid)
					.field({
						city: true
					})
					.get()
				city = userRes.data[0].city
			}
			let result = await this.service.goods.getGoodsProfitList({
				startDate,
				endDate, 
				order, 
				keyword,
				city
			})
			return {
				code: 200,
				result
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async createExchangeCode() {
		let {
			goodsId,
			skuId,
			count,
			salesman,
			salesmanPhone,
			settlementPrice,
			city
		} = this.ctx.data
		if (!goodsId || !skuId || !count || !parseInt(count) || parseInt(count) <= 0 || !salesman || !salesmanPhone
			|| (settlementPrice && (!parseInt(settlementPrice) || parseInt(settlementPrice) < 0))) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try{
			await this.service.goods.createExchangeCode({
				goodsId,
				skuId,
				count: parseInt(count),
				salesman,
				salesmanPhone,
				settlementPrice,
				city
			})
			return {
				code: 200
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//添加限时秒杀商品（修改）
	async createSeckillGoods() {
		let {
			params
		} = this.ctx.data
		if (!params.skuId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try{
			let res = await this.service.goods.createSeckillGoods({params})
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//开始限时秒杀任务
	async seckillTask() {
		let {
			seckillId
		} = this.ctx.data
		if (!seckillId) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try{
			let result = await this.service.goods.seckillTask({
				seckillId
			})
			return {
				code: 200,
				result
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
}
